import java.applet.Applet;
import java.awt.*;
import java.awt.List;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.*;

import engine.Vector;

import quadTree.BoundingBox;
import quadTree.QuadNode;
import quadTree.QuadTree;

public class QuadTest extends Applet implements Runnable,MouseMotionListener  {

	static Image dbImage;
	private Graphics dbg;
	QuadTree quad;
	LinkedList<QuadNode> intersection;
	
	public void init(){
		BoundingBox b = new BoundingBox(new Vector(0,0),new Vector(300,300));
		quad = new QuadTree(b,3);
		intersection = new LinkedList<QuadNode>();
		
		System.out.println("Tree size: " + quad.size());
		this.setBackground(Color.black);
		this.addMouseMotionListener(this);
		this.resize(400,400);
		
	}
	
	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		Vector UL = new Vector((float)arg0.getX()-1, (float)arg0.getY()-1);
		Vector LR = new Vector((float)arg0.getX()+1, (float)arg0.getY()+1);
		BoundingBox b = new BoundingBox(UL, LR);
		
		intersection = quad.findIntersect(b);
		
		if(intersection.size() > 0)
			System.out.println("Intersected: " + intersection.size() + "\n");
	}

	@Override
	public void start() {
		// define a new thread
		Thread th = new Thread (this);
		// start this thread
		th.start(); 
	}	
	
	@Override
	public void run() {
		// lower ThreadPriority
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

		// run a long while
		while (true)
		{
			// repaint the applet
			repaint();

			try
			{
				// Stop thread for 20 milliseconds
				Thread.sleep (20);		// initialize buffer
			}
			catch (InterruptedException ex)
			{
				// do nothing
			}
		}
	}

	public void update(Graphics g){
		 //initialize buffer
		if (dbImage == null)
		{

			dbImage = createImage (this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics ();

		}

		// clear screen in background
		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

		// draw elements in background
		dbg.setColor (getForeground());
		paint (dbg);

		// draw image on the screen
		g.drawImage (dbImage, 0, 0, this); 
	}
	
	public void paint(Graphics g){
		
		LinkedList<QuadNode> nodes = quad.getAllNodes();
		
		for(QuadNode n : nodes){
			g.setColor(Color.white);
			Vector UL = n.getKey().getULcorner();
			Vector LR = n.getKey().getLRcorner();
			
			for(QuadNode m : intersection){
				if(m == n)
					g.setColor(Color.red);
			}
			/*UL.scl(2);
			LR.scl(2);
			UL.add(Vector.sub(LR, UL));*/
			g.drawRect((int)UL.getX()-n.getDepth(),
					   (int)UL.getY()-n.getDepth(),
					   (int)n.getKey().getWidth()-n.getDepth(),
					   (int)n.getKey().getHeight()-n.getDepth());
		}
		//g.drawLine(0, 0, xy.x, xy.y);
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BoundingBox b = new BoundingBox(new Vector(-10,-10),new Vector(10,10));
		QuadTree quad = new QuadTree(b,1);
		System.out.println("Tree size: " + quad.size());
	}
}